package scuts.macro.feature;

import haxe.macro.Compiler;
import haxe.macro.Context;
import haxe.macro.Expr;
import scuts.macro.ExtendedContext;
import scuts.macro.Parse;

class TypeClasses 
{

  @:macro public static function register ():Array<Field> {
    trace("register type class");
    var data = Parse.parse("");
    Context.getLocalClass().get().meta.add(":build", [data], Context.currentPos());
    
    return Context.getBuildFields();
  }
  
  @:macro public static function check ():Array<Field> {
    
    trace("check " + Context.getLocalClass().toString());
    
    return if (Context.getLocalClass().get().meta.has(":typeClass")) {
      //var data = Parse.parse("scuts.macro.feature.TypeClasses.applyConstructor");
      //Context.getLocalClass().get().meta.add(":autoBuild", [data], Context.currentPos());
      var fields = scuts.traits.Traits.register(true);
      fields;
    } else {
      Context.getBuildFields();
    }
    
    
  }
  
  public static function enable () {
    var cps = Context.getClassPath();
    var types = [];
    for (i in cps) {
      if (i == "" || i.indexOf("std") != -1) continue;
      trace(i);
      types = types.concat(ExtendedContext.getTypesFromClasspath(i));
    }
    
    for (t in types) {
      //trace(t.name);
      Compiler.addMetadata("@:build(scuts.macro.feature.TypeClasses.check())", t.name);
    }
    
  }
  
}